// Copyright © yanghy. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and limitations under the License.

package codegen

import (
	"encoding/json"
	"fmt"
	"github.com/energye/designer/designer"
	"github.com/energye/designer/pkg/logs"
	"github.com/energye/designer/project"
	"github.com/energye/designer/uigen/bean"
	"os"
	"path/filepath"
)

// 根据UI文件生成Go代码
func runGenerateCode(uiGenData designer.TUIGenerationData) error {
	logs.Debug("运行 UI 布局代码生成")
	formTab := uiGenData.Component.FormTab
	uiFilePath := filepath.Join(project.Path(), project.Project().Package, formTab.UIFile())
	// 读取并解析UI文件
	data, err := os.ReadFile(uiFilePath)
	if err != nil {
		return fmt.Errorf("读取UI文件失败: %w", err)
	}
	// 解析UI文件
	var uiComponent bean.TUIComponent
	if err := json.Unmarshal(data, &uiComponent); err != nil {
		return fmt.Errorf("解析UI文件失败: %w", err)
	}
	// 生成自动代码文件 (xxx.ui.go)
	if err := generateAutoCode(formTab, &uiComponent); err != nil {
		return fmt.Errorf("生成自动代码失败: %w", err)
	}
	// 生成用户代码文件 (xxx.go) - 仅当文件不存在时
	if err := generateUserCode(formTab, &uiComponent); err != nil {
		return fmt.Errorf("生成用户代码失败: %w", err)
	}

	return nil
}

// 更新自引用
func runUpdateSelf(uiGenData designer.TUIGenerationData) error {
	logs.Debug("运行更新自引用")
	doUpdateSelf(uiGenData)
	return nil
}

// 更新绑定事件
func runUpdateEvent(uiGenData designer.TUIGenerationData) error {
	logs.Debug("运行更新绑定事件")
	doUpdateEvent(uiGenData)
	return nil
}
